package io.gitee.yaoshanli.engine.service.impl;

import io.gitee.yaoshanli.engine.db.JwDbHelper;
import io.gitee.yaoshanli.engine.service.BaseService;
import io.gitee.yaoshanli.engine.service.ICodeService;
import io.gitee.yaoshanli.engine.utils.DateUtils;
import io.gitee.yaoshanli.engine.utils.StringUtils;
import io.gitee.yaoshanli.engine.model.WfCodeModel;

import java.util.List;

/**
 * @author 姚善利
 * @classname org.jwflow.core.service.impl
 * @description
 * @date 2022/9/21 9:41
 */
public class CodeService extends BaseService<WfCodeModel> implements ICodeService {

    @Override
    public boolean checkFlow(WfCodeModel wfCodeModel, String idOrCode) {
        if(wfCodeModel.getId().equals(idOrCode)||wfCodeModel.getCode().equals(idOrCode))
        {
            return true;
        }
        return false;
    }

    @Override
    public boolean saveOrUpdate(WfCodeModel wfCode) {
        String sql="select * from jw_wf_code where id=?";
        WfCodeModel wfCodeModel= JwDbHelper.queryFirst(WfCodeModel.class,sql,wfCode.getId());
        if(wfCodeModel!=null)
        {
            String json_data=wfCode.getJson_data();
            if(StringUtils.isEmpty(json_data)) {
                sql = "update jw_wf_code code =?,name =?,desc =?,status =?,order_num =?,detail_url =?,app_url =?," +
                        "main_field =?,main_table =?,update_by =?,update_time =?" +
                        " where id = ?";
                Object args[] = new Object[]{wfCode.getCode(),wfCode.getName(),wfCode.getDesc(),wfCode.getStatus(),wfCode.getOrder_num()
                        ,wfCode.getDetail_url(),wfCode.getApp_url(),wfCode.getMain_field(),wfCode.getMain_table()
                        ,wfCode.getUpdate_by(), DateUtils.getNowDate()};
                return JwDbHelper.update(sql, args) > 0;
            }
            else
            {
                //单独保存结构
                sql = "update jw_wf_code update_by =?,update_time =?,json_data =?" +
                        " where id = ?";
                Object args[] = new Object[]{wfCode.getUpdate_by(),DateUtils.getNowDate(),json_data};
                return JwDbHelper.update(sql, args) > 0;
            }
        }
        else
        {
            String id=wfCode.getId();
            if(StringUtils.isEmpty(id))
            {
                id=StringUtils.GUID();
            }
            sql = "insert into jw_wf_code (id,code,name,desc,status,order_num,detail_url," +
                    "app_url,main_field,main_table,create_by,create_time,update_by,update_time,json_data)" +
                    "        values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

            Object args[]=new Object[]{id,wfCode.getCode(),wfCode.getName(),wfCode.getDesc(),wfCode.getStatus(),wfCode.getOrder_num()
                    ,wfCode.getDetail_url(),wfCode.getApp_url(),wfCode.getMain_field(),wfCode.getMain_table(),wfCode.getCreate_by(), DateUtils.getNowDate()
                    ,wfCode.getUpdate_by(),DateUtils.getNowDate(),""};

            return JwDbHelper.update(sql,args)>0;
        }
    }

    private NodeService nodeService=new NodeService();
    private LinkService linkService=new LinkService();
    @Override
    public boolean remove(String idOrCode) {
        WfCodeModel wfCodeModel=getWfCode(idOrCode);
        String id=wfCodeModel.getId();
        String sql="select * from jw_wf_code where id=? ";
        Object[] args = new Object[]{id};
        nodeService.removeNodes(id);
        linkService.removeLinks(id);

        return JwDbHelper.update(sql,args)>0;
    }

    @Override
    public boolean removeAndTask(String idOrCode) {
        String sql="select * from jw_wf_code where id=? or code=?";
        Object[] args = new Object[]{idOrCode,idOrCode};
        return JwDbHelper.update(sql,args)>0;
    }

    @Override
    public WfCodeModel getWfCode(String idOrCode) {
        String sql="select * from jw_wf_code where id=? or code=?";
        Object[] args = new Object[]{idOrCode,idOrCode};
        return JwDbHelper.queryFirst(WfCodeModel.class,sql,args);
    }

    @Override
    public List<WfCodeModel> getWfCodePage() {
        return null;
    }
}
